﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine {
    import Engine.Classes.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;

	/**
	 * 该类实现逻辑地图坐标和实际像素地图坐标的转换
	 * 
	 */
    public class IsoMath {

        public static const DIRECTION_VECTORS:Array = [{
            x:new Vector2(1, -1),
            y:new Vector2(-1, -1)
        }, {
            x:new Vector2(1, 1),
            y:new Vector2(1, -1)
        }, {
            x:new Vector2(-1, 1),
            y:new Vector2(1, 1)
        }, {
            x:new Vector2(-1, -1),
            y:new Vector2(-1, 1)
        }];

        public static function tilePosToPixelPos(_arg1:Number, _arg2:Number, _arg3:Number=0, _arg4:Boolean=false):Vector2{
            var _local5:Number;
            var _local6:Number;
            var _local7:World = World.getInstance();
            var _local8:Vector2 = new Vector2();
			
			// 地图的旋转角度
            var _local9:int = (_arg4) ? _local7.getRotation() : Constants.ROTATION_0;
			
            var _local10:int = _local7.getGridWidth();
            var _local11:int = _local7.getGridHeight();
            switch (_local9){
                case Constants.ROTATION_0:
                    _local8.x = ((_local10 * Constants.TILE_WIDTH) / 2);
                    _local8.y = (_local11 * Constants.TILE_HEIGHT);
                    break;
                case Constants.ROTATION_90:
                    _local8.x = 0;
                    _local8.y = ((_local11 * Constants.TILE_HEIGHT) / 2);
                    break;
                case Constants.ROTATION_180:
                    _local8.x = ((_local10 * Constants.TILE_WIDTH) / 2);
                    _local8.y = 0;
                    break;
                case Constants.ROTATION_270:
                    _local8.x = (_local10 * Constants.TILE_WIDTH);
                    _local8.y = ((_local11 * Constants.TILE_HEIGHT) / 2);
                    break;
            };
            var _local12:Object = DIRECTION_VECTORS[_local9];
            _local5 = ((((_local12.x.x * _arg1) + (_local12.y.x * _arg2)) * (Constants.TILE_WIDTH / 2)) + _local8.x);
            _local6 = ((((_local12.x.y * _arg1) + (_local12.y.y * _arg2)) * (Constants.TILE_HEIGHT / 2)) + _local8.y);
            return (new Vector2(_local5, _local6));
        }
        public static function screenPosToTilePos(_arg1:Number, _arg2:Number, _arg3:Boolean=false):Vector2{
            var _local4:Number;
            var _local5:Number;
            var _local6:Point = IsoMath.stageToViewport(new Point(_arg1, _arg2));
            _arg1 = _local6.x;
            _arg2 = _local6.y;
            var _local7:World = World.getInstance();
            var _local8:int = _local7.getGridWidth();
            var _local9:int = _local7.getGridHeight();
            var _local10:int = (_arg3) ? _local7.getRotation() : Constants.ROTATION_0;
            var _local11:Vector3 = new Vector3();
            switch (_local10){
                case Constants.ROTATION_0:
                    _local11.x = ((_local8 * Constants.TILE_WIDTH) / 2);
                    _local11.y = (_local9 * Constants.TILE_HEIGHT);
                    break;
                case Constants.ROTATION_90:
                    _local11.x = 0;
                    _local11.y = ((_local9 * Constants.TILE_HEIGHT) / 2);
                    break;
                case Constants.ROTATION_180:
                    _local11.x = ((_local8 * Constants.TILE_WIDTH) / 2);
                    _local11.y = 0;
                    break;
                case Constants.ROTATION_270:
                    _local11.x = (_local8 * Constants.TILE_WIDTH);
                    _local11.y = ((_local9 * Constants.TILE_HEIGHT) / 2);
                    break;
            };
            var _local12:Object = DIRECTION_VECTORS[_local10];
            _arg1 = ((_arg1 - _local11.x) / (Constants.TILE_WIDTH / 2));
            _arg2 = ((_arg2 - _local11.y) / (Constants.TILE_HEIGHT / 2));
            _local5 = (((_arg2 / _local12.x.y) - (_arg1 / _local12.x.x)) / ((_local12.y.y / _local12.x.y) - (_local12.y.x / _local12.x.x)));
            _local4 = ((_arg1 - (_local12.y.x * _local5)) / _local12.x.x);
            return (new Vector2(_local4, _local5));
        }
        public static function getPixelDeltaFromTileDelta(_arg1:Number, _arg2:Number, _arg3:Boolean=true):Vector2{
            var _local4:World = World.getInstance();
            var _local5:Vector2 = new Vector2();
            if (_arg3){
                switch (_local4.getRotation()){
                    case Constants.ROTATION_0:
                        _local5.x = (((_arg1 - _arg2) / 2) * Constants.TILE_WIDTH);
                        _local5.y = ((-((_arg1 + _arg2)) / 2) * Constants.TILE_HEIGHT);
                        break;
                    case Constants.ROTATION_90:
                        _local5.x = (((_arg1 + _arg2) / 2) * Constants.TILE_WIDTH);
                        _local5.y = (((_arg1 - _arg2) / 2) * Constants.TILE_HEIGHT);
                        break;
                    case Constants.ROTATION_180:
                        _local5.x = (((_arg2 - _arg1) / 2) * Constants.TILE_WIDTH);
                        _local5.y = (((_arg1 + _arg2) / 2) * Constants.TILE_HEIGHT);
                        break;
                    case Constants.ROTATION_270:
                        _local5.x = ((-((_arg1 + _arg2)) / 2) * Constants.TILE_WIDTH);
                        _local5.y = (((_arg2 - _arg1) / 2) * Constants.TILE_HEIGHT);
                        break;
                };
            } else {
                _local5.x = (((_arg1 - _arg2) / 2) * Constants.TILE_WIDTH);
                _local5.y = ((-((_arg1 + _arg2)) / 2) * Constants.TILE_HEIGHT);
            };
            return (_local5);
        }
        public static function getRotatedDirection(_arg1:int, _arg2:Boolean=false):int{
            var _local3:int;
            var _local4:World = World.getInstance();
            if (_arg2 == false){
                _local3 = (_arg1 - _local4.getRotation());
                if (_local3 < 0){
                    _local3 = (_local3 + 4);
                };
            } else {
                _local3 = (_arg1 + _local4.getRotation());
            };
            return ((_local3 % 4));
        }
        public static function getRotatedPosition(_arg1:Vector3, _arg2:Boolean=false):Vector3{
            var _local3:Vector3 = new Vector3();
            var _local4:World = World.getInstance();
            var _local5:int = _local4.getGridWidth();
            var _local6:int = _local4.getGridHeight();
            switch (_local4.getRotation()){
                case Constants.ROTATION_0:
                    _local3.x = _arg1.x;
                    _local3.y = _arg1.y;
                    break;
                case Constants.ROTATION_90:
                    _local3.x = _arg1.y;
                    _local3.y = ((_local6 - 1) - _arg1.x);
                    break;
                case Constants.ROTATION_180:
                    _local3.x = ((_local5 - 1) - _arg1.x);
                    _local3.y = ((_local6 - 1) - _arg1.y);
                    break;
                case Constants.ROTATION_270:
                    _local3.x = ((_local5 - 1) - _arg1.y);
                    _local3.y = _arg1.x;
                    break;
            };
            _local3.z = _arg1.z;
            if (_arg2){
                if (_local3.x < 0){
                    _local3.x = 0;
                };
                if (_local3.y < 0){
                    _local3.y = 0;
                };
            };
            return (_local3);
        }
        public static function generateIsoTileMatrix(_arg1:DisplayObject, _arg2:Vector2=null):Matrix{
            var _local3:World = World.getInstance();
            if (_arg2 == null){
                _arg2 = new Vector2(1, 1);
            };
            _arg2.x = (_arg2.x * Constants.TILE_WIDTH);
            _arg2.y = (_arg2.y * Constants.TILE_HEIGHT);
            var _local4:Point = new Point((_arg2.x / (_arg1.width * Math.SQRT2)), (_arg2.y / (_arg1.height * Math.SQRT2)));
            var _local5:Matrix = new Matrix();
            _local5.translate((-(_arg1.width) / 2), (-(_arg1.height) / 2));
            _local5.rotate((((-45 + (_local3.getRotation() * 90)) * Math.PI) / 180));
            _local5.translate(((_arg1.width / 2) * Math.SQRT2), ((_arg1.height / 2) * Math.SQRT2));
            _local5.scale(_local4.x, _local4.y);
            return (_local5);
        }
        public static function stageToViewport(_arg1:Point):Point{
            var _local2:Matrix = GlobalEngine.viewport.getTransformMatrix().clone();
            _local2.invert();
            var _local3:Point = _arg1.clone();
            _local3 = _local2.transformPoint(_local3);
            return (_local3);
        }
        public static function viewportToStage(_arg1:Point):Point{
            var _local2:Point = _arg1.clone();
            _local2 = GlobalEngine.viewport.getTransformMatrix().transformPoint(_local2);
            return (_local2);
        }

    }
}//package Engine 
